package com.example.administrator.liveapp.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.ReservationTab_1RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.ReservationTab_2RecyclerViewAdapter;
import com.example.administrator.liveapp.adapter.ReservationViewPagerAdapter;
import com.example.administrator.liveapp.bean.LiveOnlineBean;
import com.example.administrator.liveapp.bean.OfflineActivitiesBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;
import com.zhy.view.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的直播预约、活动报名activity
 */

public class MyReservationPage extends AppCompatActivity implements View.OnClickListener{

    private ViewPager reservation_viewPager;
    private List<View> viewContainter;
    private ViewPagerIndicator id_indicator;
    private List<String> mDatas;
    View reservation_ViewPager_tab1;
    View reservation_ViewPager_tab2;
    private RecyclerView reservation_tab1_Recycle, reservation_tab2_Recycle;
    private TextView no_content1,no_content2;
    private String consumerId;//用户Id
    private String consumerName;//用户名称
    private OfflineActivitiesBean offlineBean;//线下活动
    private LiveOnlineBean online;//在线直播

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
    }

    protected void initView() {
        setContentView(R.layout.activity_my_reservation);
        Intent intent = getIntent();
        consumerId = intent.getStringExtra("consumerId");
        consumerName = intent.getStringExtra("consumerName");
        reservation_viewPager = (ViewPager) findViewById(R.id.reservation_viewpager);
        id_indicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
        findViewById(R.id.appointment_returns).setOnClickListener(this);
        reservation_ViewPager_tab1 = LayoutInflater.from(this).inflate(R.layout.reservation_tab1_recyle, null);
        reservation_ViewPager_tab2 = LayoutInflater.from(this).inflate(R.layout.reservation_tab2_recyle, null);
        no_content1 = (TextView) reservation_ViewPager_tab1.findViewById(R.id.no_content1);
        no_content2 = (TextView) reservation_ViewPager_tab2.findViewById(R.id.no_content2);
        reservation_tab1_Recycle = (RecyclerView) reservation_ViewPager_tab1.findViewById(R.id.reservation_tab1_Recycle);
        reservation_tab2_Recycle = (RecyclerView) reservation_ViewPager_tab2.findViewById(R.id.reservation_tab2_Recycle);
        mDatas = new ArrayList<>();
        mDatas.add("线下活动");
        mDatas.add("在线直播");
        viewContainter = new ArrayList<>();
        viewContainter.add(reservation_ViewPager_tab1);
        viewContainter.add(reservation_ViewPager_tab2);
        ReservationViewPagerAdapter reservationViewPagerAdapter = new ReservationViewPagerAdapter(viewContainter);
        id_indicator.setVisibleTabCount(2);//设置可见Tab数量
        id_indicator.setTabItemTitles(mDatas);//设置tab标题
        reservation_viewPager.setAdapter(reservationViewPagerAdapter);
        id_indicator.setViewPager(reservation_viewPager, 0);//绑定ViewPager

        initData();
    }

    public Context context;

    private void initData() {
        String url = AppUrl.URLS + AppUrl.ListOrderURL;
        String url2 = AppUrl.URLS + AppUrl.ListOrder2URL;
        Map<String, String> map = new HashMap<>();
        map.put("consumerId", consumerId);
        XUtils.Get(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                offlineBean = gson.fromJson(result, OfflineActivitiesBean.class);
                if(offlineBean.getCode()==4401){
                    no_content1.setVisibility(View.VISIBLE);
                }
                //reservation_tab1_Recycle
                FullyLinearLayoutManager linerLayoutManager = new FullyLinearLayoutManager(MyReservationPage.this);
                linerLayoutManager.setScrollEnabled(true);
                reservation_tab1_Recycle.setHasFixedSize(true);
                reservation_tab1_Recycle.setLayoutManager(linerLayoutManager);
                ReservationTab_1RecyclerViewAdapter tab1 = new ReservationTab_1RecyclerViewAdapter(offlineBean.getActivity());
                reservation_tab1_Recycle.setAdapter(tab1);
                tab1.setOnItemClickListener(new ReservationTab_1RecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        String Url = offlineBean.getActivity().get(position).getActivityimgurl();
                        String ImageUrl = Url.substring(1, Url.indexOf("|"));
                        Intent intent = new Intent();
                        intent.putExtra("ImageUrl",ImageUrl+"");
                        intent.putExtra("NeedPay",offlineBean.getActivity().get(position).getNeedPay()+"");
                        intent.putExtra("consumerId", consumerId);
                        intent.putExtra("consumerName", consumerName);
                        intent.putExtra("time", offlineBean.getActivity().get(position).getVacatetime());
                        intent.putExtra("endtime", offlineBean.getActivity().get(position).getActivityendtime());
                        intent.putExtra("address", offlineBean.getActivity().get(position).getActivityplace());
                        intent.putExtra("activityId", offlineBean.getActivity().get(position).getActivityid());
                        intent.setClass(MyReservationPage.this, CancellationPage.class);
                        startActivity(intent);
                        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                        finish();
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
        Map<String, String> map2 = new HashMap<>();
        map2.put("consumerId", consumerId);
        XUtils.Get(url2, map2, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                online = gson.fromJson(result, LiveOnlineBean.class);
                if(online.getCode()==4501){
                    no_content2.setVisibility(View.VISIBLE);
                }
                //reservation_tab2_Recycle
                FullyLinearLayoutManager linerLayoutManager2 = new FullyLinearLayoutManager(MyReservationPage.this);
                linerLayoutManager2.setScrollEnabled(true);
                reservation_tab2_Recycle.setHasFixedSize(true);
                reservation_tab2_Recycle.setLayoutManager(linerLayoutManager2);
                ReservationTab_2RecyclerViewAdapter tab2 = new ReservationTab_2RecyclerViewAdapter(online.getVideo(), (MyReservationPage) context);
                reservation_tab2_Recycle.setAdapter(tab2);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.appointment_returns:
                finish();
                break;
        }
    }
}
